//
//  TSImageCardCollectionViewController.swift
//  TSListDemo
//
//  Created by qian on 2020/11/6.
//  Copyright © 2020 dvlproad. All rights reserved.
//

import UIKit
import SnapKit

class TSImageCardCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    fileprivate let identifier = "CQTableViewCardCollectionViewCell"
    var collectionView: CQImageCardCollectionView?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        requestData(false)
    }
    
    private func setupViews() {
        let collectionViewFlowLayout = CQTTCardCollectionViewFlowLayout.init(leftRightPadding: 10, topPadding: 30, bottomPadding: 30)
        collectionView = CQImageCardCollectionView(frame: CGRect.zero, collectionViewLayout: collectionViewFlowLayout)
        collectionView?.backgroundColor = #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)
        collectionView?.scrollsToTop = false
        
        collectionView?.register(CQImageCardCollectionViewCell.self, forCellWithReuseIdentifier: identifier)
        collectionView?.dataSource = self
        collectionView?.delegate = self
        self.view.addSubview(collectionView!)
        collectionView?.snp_makeConstraints({ (make) in
            make.centerX.equalTo(self.view)
            make.left.equalTo(self.view).offset(10)
            make.top.equalTo(self.view).offset(100)
            make.bottom.equalTo(self.view).offset(-40)
        })
        
        collectionView?.layoutIfNeeded()
        
        
        collectionView?.addPanGest()
        collectionView?.panCompleteBlock = { (likeType: CQCardLikeType, bCell: CQBaseCardCollectionViewCell) -> (Void) in
            
        }
    }
    
    fileprivate func requestData(_ refreshNext: Bool) {
        var array = [UIImage]()
        for i in 0..<10 {
            guard let image = UIImage(named: "image\(i)") else { return }
            array.append(image)
        }
        
        self.collectionView?.addDataModels(array: array)
    }
    
    
    // MARK: - UICollectionViewDataSource
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.collectionView!.showDataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! CQImageCardCollectionViewCell
        cell.indexPathItem = indexPath.row
        let image: UIImage = self.collectionView!.showDataSource[indexPath.row] as! UIImage
        cell.imageView.image = image
        return cell
    }
}
